package com.sd.coupon.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sd.coupon.entity.Collection;
import com.sd.coupon.entity.ItemCoupon;
import com.sd.coupon.model.req.CollectionPageReq;
import com.sd.coupon.model.req.GoodsPageReq;
import com.sd.coupon.service.MBrowseService;
import com.sd.coupon.service.MCollectionService;
import com.sd.coupon.service.MItemCouponService;
import com.sd.coupon.util.rest.RestResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Set;

@Controller
public class CollectionController {

    @Resource
    MItemCouponService itemCouponService;

    @Resource
    MCollectionService collectionService;

    @Resource
    MBrowseService browseService;

    @RequestMapping(value = "/collect", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse collect(@RequestBody  Collection collection) {

        if (collection == null || collection.getNumIid() == null) {
            return RestResponse.failure("参数异常");
        }

        Boolean res = collectionService.addOrDeleteNumIid(collection);
        return RestResponse.success(res);
    }

    @RequestMapping(value = "/collect/list", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse collectList(@RequestBody  CollectionPageReq collection) {

        if (collection == null || collection.getUserId() == null) {
            return RestResponse.failure("参数异常");
        }
        Set<Long> numIidList = collectionService.getNumIidListByUserId(collection.getUserId());

        if (numIidList != null && numIidList.size() > 0) {
            GoodsPageReq req = new GoodsPageReq(collection);
            req.setNumIidList(numIidList);
            IPage<ItemCoupon> res = itemCouponService.getItemCouponPage(req);
            return RestResponse.success(res);
        }
        return RestResponse.success(null);
    }

    @RequestMapping(value = "/browse/list", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse browseList(@RequestBody  CollectionPageReq collection) {

        if (collection == null || collection.getUserId() == null) {
            return RestResponse.failure("参数异常");
        }
        Set<Long> numIidList = browseService.getNumIidListByUserId(collection.getUserId());

        if (numIidList != null && numIidList.size() > 0) {
            GoodsPageReq req = new GoodsPageReq(collection);
            req.setNumIidList(numIidList);
            IPage<ItemCoupon> res = itemCouponService.getItemCouponPage(req);
            return RestResponse.success(res);
        }
        return RestResponse.success(null);
    }

}
